package com.holdtime.jtt809dataservice.client.process;

import com.holdtime.common.utils.general.ApiResponseModel;
import com.holdtime.common.utils.general.BaseResultCode;
import com.holdtime.jtt809dataservice.business.api.ClientLinkService;
import com.holdtime.jtt809dataservice.client.runnable.ClientScheduledService;
import com.holdtime.jtt809dataservice.common.bean.ClientUpConnectRspDTO;
import com.holdtime.jtt809dataservice.common.domain.LinkStatusManager;
import com.holdtime.jtt809dataservice.common.event.ClientMessageReceiveEvent;
import com.holdtime.jtt809dataservice.common.process.ClientProtocolProcessor;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import protocol.jtt809.JTT809Packet;
import protocol.jtt809.body.RawBody;
import protocol.jtt809.body.UpConnectRsp;

import java.util.concurrent.ScheduledFuture;

/**
 * author: sunwei
 * Date: 2025/3/18 17:22
 * Description:
 */
@Slf4j
@Component
public class ClientUpConnectRspProcessor implements ClientProtocolProcessor {
    @Resource
    private ClientLinkService clientLinkService;
    @Resource
    private ClientScheduledService clientScheduledService;

    @Override
    public int getMsgID() {
        return UpConnectRsp.MSG_ID;
    }

    @Override
    public void process(ClientMessageReceiveEvent event) {
        JTT809Packet<RawBody> source = event.getSource();
        RawBody body = source.getBody();
        UpConnectRsp upConnectRsp = new UpConnectRsp();
        upConnectRsp.from(body.getRaw());
        log.info("收到上级平台发送的主链路登陆应答消息，接入码:{},应答结果:{}", source.getMsgGnsscenterid(), upConnectRsp.getResult());
        ClientUpConnectRspDTO clientUpConnectRspDTO = new ClientUpConnectRspDTO();
        clientUpConnectRspDTO.setResult(upConnectRsp.getResult());
        clientUpConnectRspDTO.setVeryifyCode(upConnectRsp.getVerifyCode());
        clientUpConnectRspDTO.setJrm(source.getMsgGnsscenterid());
        ApiResponseModel<?> model = clientLinkService.dealUpConnectRsp(clientUpConnectRspDTO);
        if (model.getResultCode() == BaseResultCode.SUCCESS.getCode()) {
            byte result = upConnectRsp.getResult();
            ScheduledFuture<?> clientUpConnectReqs = clientScheduledService.getClientUpConnectReqs(source.getMsgGnsscenterid());
            switch (result) {
                case 0x00:
                    //取消周期发送主链路连接请求
                    if (clientUpConnectReqs != null) {
                        clientUpConnectReqs.cancel(true);
                    }
                    LinkStatusManager.getCurrent().putMainLinkStatus(source.getMsgGnsscenterid(), 1);
                    break;
                case 0x01:
                case 0x02:
                case 0x03:
                case 0x04:
                    //取消周期发送主链路连接请求
                    if (clientUpConnectReqs != null) {
                        clientUpConnectReqs.cancel(true);
                    }
                    break;
                default:
                    break;
            }

        } else {
            log.error("处理主链路连接请求异常!msg:{}", model.getMsg());
        }
    }
}
